import re
import MySQLdb
import urllib2
import cookielib
from htmlentitydefs import name2codepoint as n2cp

debug=2

class kit():
	def checksum(self,st):
		return reduce(lambda x,y:x+y, map(ord, st))

	def create_browser(self):
		cj=cookielib.CookieJar()
		cj.clear()
		CHandler = urllib2.HTTPCookieProcessor(cj)
#		phandle=urllib2.ProxyHandler({"tcp":"127.0.0.1:9050"})
		browser = urllib2.build_opener(CHandler)
		browser.addheaders = [('User-agent', 'Mozilla/5.0')]
		urllib2.install_opener(browser)
		return browser

	def connect_db(self,host,port,uname,password,db):
		database = MySQLdb.connect(host=host,port=port,user=uname,passwd=password,db=db,use_unicode=True,charset="utf8")
		#MySQLdb.escape_string("'")
		return database
	
	def decode_htmlentities(self,string):
		entity_re = re.compile("&(#?)(\d{1,5}|\w{1,8});")
		return entity_re.subn(self.substitute_entity, string)[0]
	
	def substitute_entity(self,match):
		ent = match.group(2)
		if match.group(1) == "#":
			return unichr(int(ent))
		else:
			cp = n2cp.get(ent)

			if cp:
				return unichr(cp)
			else:
				return match.group()
	
	def browser_open(self,browser,link):
		try:
			response=browser.open(link)
		except:
			print 'EXCEPTION OCCURED ON :'+ link +'. But thats ok..'
			return self.browser_open(browser,link)
		return response.read()

	def sortanduniq(self,input,excludes=[]):
		output = []
		for x in input:
			if x not in output and x not in excludes:
				output.append(x)
		output.sort()
		return output
